﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using DALHelper.Business;

namespace ResortManager
{
    /// <summary>
    /// Interaction logic for pgLookup.xaml
    /// </summary>
    public partial class pgLookupTrans : Page
    {
        private ObservableCollection<Transaction> m_dtTransactionDataSrc;

        public void UpdateData()
        {
            ICollectionView view = GetTransactionDataView();
            view.Filter = SearchFilter;
            dgTransaction.ItemsSource = view;
        }

        public Transaction SelectedTransaction
        {
            get
            {
                return (dgTransaction.SelectedItem as Transaction);
            }
        }

        private ICollectionView GetTransactionDataView()
        {

            m_dtTransactionDataSrc = new ObservableCollection<Transaction>(
                       ((App)App.Current).BUSManager.Transaction.GetInfoTrans(string.Empty));

            ICollectionView view = CollectionViewSource.GetDefaultView(m_dtTransactionDataSrc);
            view.Filter = SearchFilter;
            return view;
        }

        public pgLookupTrans()
        {
            InitializeComponent();
        }

        private bool SearchFilter(object sender)
        {
            Transaction trans = sender as Transaction;

            if (trans != null)
            {
                string sLoginName = txtLoginName.Text.Trim();
                string sRepresenter = txtRepresenter.Text.Trim();
                
                bool b1 = string.IsNullOrEmpty(sLoginName)
                        || trans.UserName.Contains(sLoginName);
                bool b2 = string.IsNullOrEmpty(sRepresenter)
                        || trans.Representer.Contains(sRepresenter);
                return b1 && b2;
            }

            return false;
        }

        private void txtRepresenter_TextChanged(object sender, TextChangedEventArgs e)
        {
            ICollectionView view = (dgTransaction.ItemsSource as ICollectionView);
            if (view != null)
            {
                view.Refresh();
            }

        }
    }
}
